<template>
  <div class="recommend">
    <van-pull-refresh
      v-model="isLoading"
      @refresh="onRefresh"
      success-text="刷新成功"
    >
      <div
        class="item"
        v-for="item in list"
        :key="item.id"
        @click="$router.push({ path: `/detail?id=${item.id}` })"
      >
        <!-- <div
      class="item"
      v-for="item in list"
      :key="item.id"
      @click="$router.push({ path: `/detail/${item.id}` })"
    > -->
        <div class="top">
          <!-- <div class="left">1</div> -->
          <img :src="item.avatar" alt="" />
          <div class="right">
            <div class="title">{{ item.stem }}</div>
            <div class="username">
              <span>{{ item.creatorName }}</span>
              <span>|</span>
              <span>{{ item.createdAt }}</span>
            </div>
          </div>
        </div>
        <div class="middle">
          {{ item.content }}
        </div>
        <div class="bottom">
          <span>点赞 {{ item.likeCount }}</span>
          <span>|</span>
          <span>浏览 {{ item.views }}</span>
        </div>
      </div>
    </van-pull-refresh>
  </div>
</template>
<script>
import axios from "axios";
export default {
  name: "HmArticle",
  data() {
    return {
      list: [],
      isLoading: false,
    };
  },
  created() {
    this.getList();
  },

  methods: {
    async getList() {
      this.list = [];
      const res = await axios({
        url: "http://interview-api-t.itheima.net/h5/interview/query",
        headers: {
          Authorization: `Bearer ${localStorage.getItem("token")}`,
        },
      });
      this.list = res.data.data.rows;

      console.log(this.list);
    },
    // 下拉刷新覆盖之前的数据

    async onRefresh() {
      this.isLoading = true;

      try {
        await this.getList();
        console.log("刷新数据成功");
      } catch (err) {
        console.log("刷新数据失败", err);
      } finally {
        this.isLoading = false;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.recommend {
  margin-top: 60px;
}
.item {
  border: 1px solid #000;
  padding: 10px;
  margin: 10px;

  .top {
    display: flex;

    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }

    .right {
      flex: 1;
      margin-left: 10px;
    }
  }
}
// 超过两行显示省略号
.middle {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
</style>
